package be.fedict.eid.diagnostics.tests;

import java.util.List;

import javax.smartcardio.CardTerminal;

import be.fedict.eid.diagnostics.DiagnosticTest;
import be.fedict.eid.diagnostics.TestFailedException;
import be.fedict.eid.diagnostics.annotations.TestName;
import be.fedict.eid.diagnostics.helper.DataTreeBuilder;
import be.fedict.eid.diagnostics.system.OSException;
import be.fedict.eid.diagnostics.system.OperatingSystem;

@TestName("PCSC: Retrieving card readers")
public class PCSCReaderListTest extends DiagnosticTest {

	@Override
	protected boolean runTest() throws TestFailedException {
		
		OperatingSystem os;
		try {
			os = new OperatingSystem();
		} catch (OSException e) {
			throw new TestFailedException("Could not establish type of Operating System.");
		}
		
		List<CardTerminal> readers = os.getPCSCManager().readerList();
		for (CardTerminal reader : readers) {
			rootNode.add(DataTreeBuilder.buildNode("name", reader.getName()));
		}
		if (readers.isEmpty())
			throw new TestFailedException("No readers found.");
		
		return true;
	}

	@Override
	protected String getRootName() {
		return "pcsc_readers";
	}

}
